package message

import (
	"encoding/json"
	"gitee.com/cooge/impush/gutil"
	"gitee.com/cooge/impush/util"
)

var md *gutil.LinkMap

func init() {
	md = gutil.NewLinkMap()
}
func LoginSuccess() []byte {

	m := md.Get("LOGINSUCCESS")

	if m != nil {

		return m.([]byte)
	}

	var msg_ Message

	head := make(Head)
	head[TYPE] = LOGIN
	head[STATUS] = SUCCESS
	msg_.Heads = head
	data :=msg_.MessageToByte()
	md.Add("LOGINSUCCESS", data)

	return data
}

func Live() []byte {

	m := md.Get("LIVE")

	if m != nil {
		return m.([]byte)
	}
	var msg Message
	head := make(Head)
	head[TYPE] = LIVE
	msg.Heads = head

	data :=msg.MessageToByte()
	md.Add("LIVE", data)
	return data
}

func FindMachine(machineId string) []byte {

	m := md.Get("FINDMACHINE")
	if m != nil {

		return m.([]byte)
	}

	var msg Message

	head := make(Head)
	head[TYPE] = CLUSTER
	head[ACTION] = FIND
	head[MACHINEID] = machineId
	msg.Heads = head

	data := msg.MessageToByte()
	md.Add("FINDMACHINE", data)
	return data
}
func FoundMachine(machineId string) []byte {
	//	m := md.Get("FOUNDMACHINE")
	//	if m != nil {
	//		return m.([]byte)
	//	}
	var msg Message

	head := make(Head)
	head[TYPE] = CLUSTER
	head[ACTION] = FOUND
	head[MACHINEID] = machineId
	msg.Heads = head

	data := msg.MessageToByte()
	//	md.Add("FOUNDMACHINE", data)
	return data

}
func ClusterLive() []byte {
	var msg Message

	head := make(Head)
	msg.Heads = head

	head[TYPE] = CLUSTER
	head[ACTION] = LIVE
	data := msg.MessageToByte()
	return data

}
func ClusterSuccess(machineId string, machines string) []byte {
	var msg Message
	head := make(Head)
	msg.Heads = head

	head[TYPE] = CLUSTER
	head[ACTION] = SUCCESS
	head[MACHINEID] = machineId

	msg.Body = []byte(machines)
	data := msg.MessageToByte()
	return data

}
func ClusterRevUser(machineId string) []byte {
	var msg Message
	head := make(Head)
	msg.Heads = head
	head[TYPE] = CLUSTER
	head[ACTION] = REVUSER
	head[MACHINEID] = machineId
	data := msg.MessageToByte()
	return data

}
func ClusterDelUser(machineId string) []byte {
	var msg Message
	head := make(Head)
	msg.Heads = head
	head[TYPE] = CLUSTER
	head[ACTION] = DELUSER
	head[MACHINEID] = machineId
	data := msg.MessageToByte()
	return data

}

/*根据用户ID 向用户发送信息*/
func GetSystemMessage(userid string, mesg string) *Message {
	var msg Message
	head := make(Head)
	msg.Heads = head
	head[TYPE] = MESSAGE
	head[TO] = userid
	head[FROM] = SYSTEM
	msg.Body = []byte(mesg)
	return &msg
}

/**向所有用户发送信息**/
func GetAllUserMessage(mesg string) *Message {
	var msg Message
	head := make(Head)
	msg.Heads = head
	head[TYPE] = MESSAGE
	head[TO] = ALLUSERMSG
	head[FROM] = SYSTEM
	msg.Body = []byte(mesg)
	return &msg
}

func StartCluster(machineId string) []byte {


	var msg Message
	head := make(Head)
	msg.Heads = head

	head[TYPE] = CLUSTER
	head[ACTION] = START
	head[MACHINEID] = machineId

	data := msg.MessageToByte()

	return data
}

func SendSuccess(msgid string) *Message {
	var msg Message
	head := make(Head)
	msg.Heads = head

	head[TYPE] = TRACK
	head[MESSAGEID] = msgid
	head[RESULT] = SUCCESS

	return &msg
}
func SendFail(msgid string) *Message {
	var msg Message
	head := make(Head)
	msg.Heads = head

	head[TYPE] = TRACK
	head[MESSAGEID] = msgid
	head[RESULT] = FAIL

	return &msg
}
func SendNoUser(msgid string) *Message {
	var msg Message

	head := make(Head)
	msg.Heads = head
	head[TYPE] = TRACK
	head[MESSAGEID] = msgid
	head[RESULT] = NO_USER

	return &msg
}

func SendTrack(msgid string, info string) *Message {
	var msg Message
	head := make(Head)
	msg.Heads = head
	head[TYPE] = TRACK
	head[MESSAGEID] = msgid
	head[RESULT] = info
	return &msg
}

func StartLogin() []byte {
	m := md.Get("STARTLOGIN")
	if m != nil {
		return m.([]byte)
	}
	var msg Message

	head := make(Head)
	msg.Heads = head
	head[TYPE] = LOGIN
	head[STATUS] = START

	data := msg.MessageToByte()
	md.Add("STARTLOGIN", data)
	return data

}

func SendAllMachines(machineId string, m *[]string) []byte {
	data, err := json.Marshal(m)
	if err != nil {
		return nil
	}
	var msg Message
	head := make(Head)
	head[TYPE] = CLUSTER
	head[ACTION] = SENDMACHINE
	head[MACHINEID] = machineId
	msg.Heads = head
	msg.Body = data
	return msg.MessageToByte()
}


func SendMachinesNoUser(userid ...string) []byte {
	var msg Message
	head := make(Head)
	head[TYPE] = CLUSTER
	head[ACTION] = NOUSER
	msg.Heads = head
	msg.Body = util.Join(userid,";")
	return msg.MessageToByte()
}




func SendClusterLiveMessage(address []string) []byte {
	var msg Message
	head := make(Head)
	head[TYPE] = CLUSTER
	head[ACTION] = LIVE
	msg.Heads = head
	msg.Body = util.Join(address,";")
	return msg.MessageToByte()
}



